package cn.edu.tyust.scms.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import cn.edu.tyust.scms.model.StuTeamRelation;
import cn.edu.tyust.scms.service.StuTeamRelationService;
import cn.edu.tyust.scms.service.StuTeamRelationServiceImpl;

/**
 * Servlet implementation class FindStuTeamRelationServlet
 */
@WebServlet("/FindStuTeamRelationServlet")
public class FindStuTeamRelationServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FindStuTeamRelationServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        String jsonStr = JSON.toJSONString(null);
        
        StuTeamRelationService stuTeamRelationService = new StuTeamRelationServiceImpl();
        Map<String, Object> map = new HashMap<String, Object>();

        String status = request.getParameter("status");
        if(status.equals("findteamleader")) {
            String teamid = request.getParameter("teamid");
            map.put("teamid", teamid);
            map.put("sturole",  "组长");
            List<StuTeamRelation> relationList = stuTeamRelationService.findStuTeamRelation(map);
            if(relationList.size()!=0) {
                jsonStr = JSON.toJSONString(relationList.get(0));
            }
        }
        
        // 使用阿里的插件，转为JSON字符串
        out.print(jsonStr);
        out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
